package inabit.inabitcompiler.productions.rule.expressions;

import inabit.compiler.parser.grammar.NonTerminal;
import inabit.compiler.parser.grammar.Production;
import inabit.compiler.parser.grammar.Symbol;
import inabit.compiler.parser.grammar.Terminal;
import inabit.data.expression.Expression;
import inabit.data.expression.LogicExpression;
import inabit.inabitcompiler.lexical.InabitTokenClass;
import inabit.inabitcompiler.parser.InabitSemanticAction;

/**K' -> or KK'*/
/**@author Rafael Rocha*/
public class K1_Rule extends Production{
	private NonTerminal K;
	private NonTerminal K1;
	
	public K1_Rule() {
		super("K'");
	}

	@Override
	protected Symbol[] production(final NonTerminal nonTerminal) {
		K = new NonTerminal("K");
		K1 = new NonTerminal("K'");
		Symbol[] symbols = {
			new Terminal(InabitTokenClass.OR),
			K,
			K1,
			new InabitSemanticAction(nonTerminal) {
				@Override
				public void execute() throws Exception {
					if(K1.code.isEmpty()) {
						nonTerminal.code = K.code;
					}else {
						Expression e = new LogicExpression(
								getExpression(K.code),
								LogicExpression.OR,
								getExpression(K1.code));
						nonTerminal.code = addExpressionAndGetCode(e);
					}
				}
			}
		};
		return symbols;
	}

}
